.PHONY : mingw ej2d linux undefined

CFLAGS = -g -Wall -Ilib -Ilib/render -Ilua -D EJOY2D_OS=$(OS) -D FONT_EDGE_HASH
LDFLAGS :=

RENDER := \
lib/render/render.c \
lib/render/carray.c \
lib/render/log.c

EJOY2D := \
lib/shader.c \
lib/lshader.c \
lib/ejoy2dgame.c \
lib/fault.c \
lib/screen.c \
lib/texture.c \
lib/ppm.c \
lib/spritepack.c \
lib/sprite.c \
lib/lsprite.c \
lib/matrix.c \
lib/lmatrix.c \
lib/dfont.c \
lib/label.c \
lib/particle.c \
lib/lparticle.c \
lib/scissor.c \
lib/renderbuffer.c \
lib/lrenderbuffer.c \
lib/lgeometry.c \
lib/lutls.c

SRC := $(EJOY2D) $(RENDER)

LUASRC := \
lua/lapi.c \
lua/lauxlib.c \
lua/lbaselib.c \
lua/lbitlib.c \
lua/lcode.c \
lua/lcorolib.c \
lua/lctype.c \
lua/ldblib.c \
lua/ldebug.c \
lua/ldo.c \
lua/ldump.c \
lua/lfunc.c \
lua/lgc.c \
lua/linit.c \
lua/liolib.c \
lua/llex.c \
lua/lmathlib.c \
lua/lmem.c \
lua/loadlib.c \
lua/lobject.c \
lua/lopcodes.c \
lua/loslib.c \
lua/lparser.c \
lua/lstate.c \
lua/lstring.c \
lua/lstrlib.c \
lua/ltable.c \
lua/ltablib.c \
lua/ltm.c \
lua/lundump.c \
lua/lutf8lib.c \
lua/lvm.c \
lua/lzio.c

EXTERNALSRC := \
external/liekkas/lk_bgm.c           \
external/liekkas/lk_decode.c        \
external/liekkas/lk_util.c          \
external/liekkas/decode/ad_mp3.c    \
external/liekkas/decode/ad_ogg.c    \
external/liekkas/decode/ad_tools.c  \
external/liekkas/decode/ad_wav.c    \
external/liekkas/openal/oal.c       \
external/lyaml/emitter_wrap.c       \
external/lyaml/lyaml.c              \
external/lyaml/parser_wrap.c        \
external/lyaml/scanner_wrap.c       \
external/lyaml/yaml/api.c           \
external/lyaml/yaml/dumper.c        \
external/lyaml/yaml/emitter.c       \
external/lyaml/yaml/loader.c        \
external/lyaml/yaml/parser.c        \
external/lyaml/yaml/reader.c        \
external/lyaml/yaml/scanner.c       \
external/lyaml/yaml/writer.c        \
external/lzma/7zCrc.c               \
external/lzma/7zCrcOpt.c            \
external/lzma/7zStream.c            \
external/lzma/Bcj2.c                \
external/lzma/Bra.c                 \
external/lzma/Bra86.c               \
external/lzma/BraIA64.c             \
external/lzma/CpuArch.c             \
external/lzma/Delta.c               \
external/lzma/llzma.c               \
external/lzma/LzAlloc.c             \
external/lzma/LzFind.c              \
external/lzma/Lzma2Dec.c            \
external/lzma/Lzma2Enc.c            \
external/lzma/Lzma86Dec.c           \
external/lzma/Lzma86Enc.c           \
external/lzma/LzmaDec.c             \
external/lzma/LzmaEnc.c             \
external/lzma/LzmaLib.c             \
external/lzma/Ppmd7.c               \
external/lzma/Ppmd7Dec.c            \
external/lzma/Ppmd7Enc.c            \
external/lzma/Sha256.c              \
external/lzma/Xz.c                  \
external/lzma/XzCrc64.c             \
external/lzma/XzDec.c               \
external/lzma/XzEnc.c               \
external/lzma/XzIn.c                \
external/sproto/lsproto.c           \
external/sproto/sproto.c            \
external/tween/easing.c             \
external/tween/leasing.c            \
external/utls/image.c               \
external/utls/lcrypt.c              \
external/utls/lmonitor.c            \
external/utls/ltimesync.c           \
external/utls/lualibs.c             \
external/utls/serialize.c           \
external/utls/lpeg/lpeg.c           \
external/platform/msvc/argparse.c   \
external/platform/msvc/filesystem.c \
external/platform/msvc/os_utls.c    


CC=gcc
UNAME=$(shell uname)
SYS=$(if $(filter Linux%,$(UNAME)),linux,\
	    $(if $(filter MINGW%,$(UNAME)),mingw,\
	    $(if $(filter Darwin%,$(UNAME)),macosx,\
	        undefined\
)))

all: $(SYS)

undefined:
	@echo "I can't guess your platform, please do 'make PLATFORM' where PLATFORM is one of these:"
	@echo "      linux mingw macosx"
	
mingw : OS := WINDOWS
mingw : TARGET := ej2d.exe
mingw : CFLAGS += -I/usr/include -I./external/platform/ -I./external/ -I./external/mpg123/include
mingw : LDFLAGS += -L/usr/bin -L./ -L./external/openal/lib/mingw -L./external/mpg123/lib/mingw -lgdi32 -lglew32 -lopengl32 -lws2_32 -lOpenAl32 -lmpg123 -lwinmm -DAL_LIBTYPE_STATIC -DSUPPORT_AUDIO_MP3 -msse3  
mingw : SRC += mingw/window.c mingw/winfw.c mingw/winfont.c

mingw : $(SRC) ej2d

winlib : OS := WINDOWS
winlib : TARGET := ejoy2d.dll
winlib : CFLAGS += -I/usr/include -I/usr/local/include --shared
winlib : LDFLAGS += -L/usr/bin -lgdi32 -lglew32 -lopengl32 -L/usr/local/bin -llua53
winlib : SRC += mingw/winfont.c lib/lejoy2dcore.c

winlib : $(SRC) ej2dlib

ej2dlib :
	$(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LDFLAGS)


linux : OS := LINUX
linux : TARGET := ej2d
linux : CFLAGS += -I/usr/include $(shell freetype-config --cflags)
linux : LDFLAGS +=  -lGLEW -lGL -lX11 -lfreetype -lm
linux : SRC += posix/window.c posix/winfw.c posix/winfont.c

linux : $(SRC) ej2d

macosx : CC := clang
macosx : OS := MACOSX
macosx : TARGET := ej2d
macosx : CFLAGS += -I/usr/include $(shell freetype-config --cflags) -D __MACOSX
macosx : LDFLAGS += -lglfw3  -framework OpenGL -lfreetype -lm -ldl
macosx : SRC += mac/example/example/window.c posix/winfw.c mac/example/example/winfont.c

macosx : $(SRC) ej2d

ej2d :
	$(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(EXTERNALSRC) $(LUASRC) $(LDFLAGS)

clean :
	-rm -f "ej2d.exe"
	-rm -f $(EXTERNALOBJ)
